import { MenuResponseData, RoleData, RoleResponseData } from './types';

// 统一接口管理
enum API {
  GET_ROLES_URL = '/admin/acl/role',
  ADD_ROLE_URL = '/admin/acl/role/save',
  UPDATE_ROLE_URL = '/admin/acl/role/update',
  DELETE_ROLE_URL = '/admin/acl/role/remove',
  GET_PERMISSION_BYROLEID_URL = '/admin/acl/permission/toAssign',
  ASSIGN_PERMISSION_URL = '/admin/acl/permission/doAssign',
}

//获取角色接口
export const reqGetAllRoles = (page: number, limit: number, roleName: string) => {
  return request.get<any, RoleResponseData>(API.GET_ROLES_URL + `/${page}/${limit}/?roleName=${roleName}`);
};
//添加或者更新角色
export const reqAddOrUpdateRole = (data: RoleData) => {
  if (data.id) {
    return request.put<any, any>(API.UPDATE_ROLE_URL, data);
  } else {
    return request.post<any, any>(API.ADD_ROLE_URL, data);
  }
};
//删除角色
export const reqDeleteRole = (id: number) => {
  return request.delete<any, any>(API.DELETE_ROLE_URL + `/${id}`);
};
//获取所有权限
export const reqGetPermissionByRoleId = (id: number) => {
  return request.get<any, MenuResponseData>(API.GET_PERMISSION_BYROLEID_URL + `/${id}`);
};
//分配权限
export const reqAssignPermission = (roleId: number, permissionId: number[]) => {
  return request.post<any, any>(API.ASSIGN_PERMISSION_URL + `/?roleId=${roleId}&permissionId=${permissionId}`);
};
